package cn.wolfcode.trip.web.controller;

import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.QiniuCloudUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {
    @Autowired
    private ITravelCommendService service;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelCommendQueryObject qo) {
        qo.setOrderBy("schedule desc");
        PageInfo<TravelCommend> pageInfo = service.selectForList(qo);
        model.addAttribute("result", pageInfo);
        return "travel/commendList";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(TravelCommend travelCommend, MultipartFile pic) {
        if (pic != null && pic.getContentType().startsWith("image") && pic.getSize() > 0) {
            String url = QiniuCloudUtil.uploadQiniu(pic);
            travelCommend.setCoverUrl(url);
        }
        service.saveOrUpdate(travelCommend);
        return "redirect:/travelCommend/list.do";

    }

}
